package se.lightsources.manual;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.foreningsgatan.modular.common.logging.Log;
import org.foreningsgatan.modular.lightsources.CacheType;
import org.foreningsgatan.modular.lightsources.LightMap;
import org.foreningsgatan.modular.lightsources.LightSource;
import org.foreningsgatan.modular.lightsources.LightSourceImpl;
import org.foreningsgatan.modular.lightsources.LightSources;
import org.foreningsgatan.modular.lightsources.RGB;

/**
 * 
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 *
 */
public class ManualTestRenderAll extends AbstractFrame {
	static protected int TILE_SIZE = 5;
	
	static protected int MAP_SIZE = 80;
	
	protected int windowSize = MAP_SIZE;
	
	protected List<LightSourceImpl> sources;
	
	protected int offsetX;
	
	protected int offsetY;
	
	protected LightSources<LightSource> lightSources;
	
	protected Log log;
	
	public ManualTestRenderAll(int width, int height) {
		super("LightSources", width, height);
	}
	
	protected void init() {
		Random random = new Random();
		
		lightSources = new LightSources<LightSource>();
		
		lightSources.setAmbientLight(new RGB(0.0f, 0.1f, 0.3f));
		
		log = new Log(this.getClass());
		
		sources = new ArrayList<LightSourceImpl>();
		
		Timer t = new Timer();
		
		for (int i = 0; i < 50; i++) {
			double x = random.nextDouble() * MAP_SIZE;
			
			double y = random.nextDouble() * MAP_SIZE;
			
			double r = random.nextDouble() * 25 + 5;
				
			LightSourceImpl source = new LightSourceImpl(x, y, r, 
					new RGB(random.nextFloat(), random.nextFloat(), random.nextFloat()), 
					CacheType.STATIC);
			
			lightSources.register(source);
		}
		
		log.debug("Generating lights took %s", t);
		
		t = new Timer();
		
		lightSources.updateStaticLights(MAP_SIZE);
		
		log.debug("Rendering static lights took %s", t);
		
		offsetX = MAP_SIZE / 2;
		
		offsetY = MAP_SIZE / 2;
	}
	
	protected LightMap render() throws Exception {
		Timer t = new Timer();
		
		LightMap map = lightSources.render(windowSize, 
				offsetX - windowSize / 2, offsetY - windowSize / 2);
		
		log.debug("Rendering took %s", t);
		
		return map;
	}
	
	protected void draw(Graphics g) {
		g.setColor(Color.BLACK);
		
		g.fillRect(0, 0, width, height);
		
		try {
			LightMap map = render();
			
			int size = map.getSize();
			
			for (int y = 0; y < size; y++) {
				for (int x = 0; x < size; x++) {
					RGB rgb = map.get(x, y);
					
					g.setColor(new Color(rgb.getR(), rgb.getG(), rgb.getB()));
					g.fillRect((x + offsetX - windowSize / 2) * TILE_SIZE, 
							(y + offsetY - windowSize / 2) * TILE_SIZE, 
							TILE_SIZE, TILE_SIZE);
				}
			}
			
			g.setColor(Color.GRAY);
			
			g.drawRect((offsetX - windowSize / 2) * TILE_SIZE, 
					(offsetY - windowSize / 2) * TILE_SIZE, 
					TILE_SIZE * windowSize, TILE_SIZE * windowSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void mousePressed(int x, int y) {
		repaint();
	}
	
	public static void main(String[] args) {
		new ManualTestRenderAll(640, 480);
	}
}
